﻿using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Media.Imaging;

namespace Rio.Framework.Pages.Viewer
{
    public partial class ViewerEditor : ControlBase
    {

        private bool _isEditorVisible;

        /// <summary>
        /// Gets the viewer.
        /// </summary>
        public RadImageEditor Viewer
        {
            get { return this.ImageViewer; }
        }

        /// <summary>
        /// Gets or sets the image.
        /// </summary>
        /// <value>
        /// The image.
        /// </value>
        public RadBitmap Image
        {
            get { return Viewer.Image; }
            set { Viewer.Image = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is editor visible.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is editor visible; otherwise, <c>false</c>.
        /// </value>
        public bool IsEditorVisible
        {
            get
            {
                return _isEditorVisible;
            }
            set 
            { 
                _isEditorVisible = value;
                if (value)
                {
                    ToolGroup.ShowAllPanes();
                }
                else
                {

                    ToolGroup.HideAllPanes();
                }
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="ViewerEditor"/> class.
        /// </summary>
        public ViewerEditor()
        {
            InitializeComponent();

            IsEditorVisible = false;
        }

        /// <summary>
        /// Saves the layout as string.
        /// </summary>
        /// <returns></returns>
        private string SaveLayoutAsString()
        {
            MemoryStream stream = new MemoryStream();
            this.Docking.SaveLayout(stream);

            stream.Seek(0, SeekOrigin.Begin);

            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }

        /// <summary>
        /// Loads the layout from string.
        /// </summary>
        /// <param name="xml">The XML.</param>
        private void LoadLayoutFromString(string xml)
        {
            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                stream.Seek(0, SeekOrigin.Begin);
                this.Docking.LoadLayout(stream);
            }
        }

    }
}
